package servlet;

import bean.User;
import dao.impl.UserDaoImpl;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import utils.c3p0Utils;

import javax.naming.Name;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author guoxiuwei
 * @Date 2020/8/18 08:26
 * @Title
 */
@WebServlet("/registerServlet")
public class registerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //防止中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取页面传来的数据
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");


        //System.out.println(uname + "->" + upwd);
        /*
        第三个参数是从页面传过来的  选择权限后  传过来对应的数据
        1 管理员
        2 经理
        3 员工
         */

        //假设现在传过来的是一个经理 正常情况下是从页面获取来的东西
        int rights = 3;
        boolean register = new UserDaoImpl().register(uname, upwd, rights);
        if(register){
            User user=new UserDaoImpl().FindUid(uname);
            int uid=user.getUid();
            request.setAttribute("uid",uid);
            request.getRequestDispatcher("/InfoPerfect.jsp").forward(request,response);


        }else {
            //这里要弹出一个窗口  内容 用户名已存在  然后返回到注册界面
            response.getWriter().println("注册失败  用户名已存在");
        }
    }
}
